local map = class("map")
local tileType = {"forest","brick","iron","water","ice","none"}

function map:init(game,w,h,size)
    self.w = w or 50
    self.h = h or 50
    self.size = size or 16
    self.game = game
    game.map = self
    self.world = game.world
    self.tileQuad = tileQuad
    self:clear()
end

function map:clear()
    self.tiles = {}
    if not SERVER then
        self:newCanvas()
    end
    for x = 1, self.w do
        for y = 1, self.h do
            self:clearTile(x,y)
        end
    end
    self:makeBorder()
    if not SERVER then
        self.game:initCam()
    end
end

function map:getData()
    return {
        w = self.w,
        h = self.h,
        tiles = self.tiles
        }
end

function map:newCanvas()
    self.map_canvas = love.graphics.newCanvas(self.w*16,self.h*16)
    self.forest_canvas = love.graphics.newCanvas(self.w*16,self.h*16)
end

function map:canvasExpand()
    self.map_canvas = love.graphics.newCanvas(self.w*16,self.h*16)
    self.forest_canvas = love.graphics.newCanvas(self.w*16,self.h*16)
    for x = 1, self.w do
        for y = 1, self.h do
            self:drawTile(x,y,self.tiles[x][y].type)
        end
    end
end


function map:saveData()
    return table.save(self:getData())
end

function map:setData(data)
    self.w = data.w
    self.h = data.h
    if not SERVER then
        self:newCanvas()
    end
    for x = 1, self.w do
        for y = 1, self.h do
            self:setTile(x,y,data.tiles[x][y].type)
        end
    end
    self:makeBorder()
    if not SERVER then
        self.game:initCam()
    end
end

function map:clearTile(x,y)
    self:setTile(x,y,6)
end

function map:makeBorder()
    if not self.world then return end
    self:removeBorder()
    local border_left = {tag = "border"}
    self.world:add(border_left,-self.size,-self.size,self.size,self.size*self.h)
    local border_bottom = {tag = "border"}
    self.world:add(border_bottom,-self.size,self.size*self.h,self.size*self.w,self.size)
    local border_right = {tag = "border"}
    self.world:add(border_right,self.size*self.w,0,self.size,self.h*self.size)
    local border_top = {tag = "border"}
    self.world:add(border_top,0,-self.size,self.size*self.w,self.size)
    self.borders = {border_left,border_right,border_top,border_bottom}
end

function map:removeBorder()
    if not self.borders then return end
    for i,v in ipairs(self.borders) do
        self.world:remove(v)
    end
end

function map:setTile(x,y,t)
    self.tiles[x] = self.tiles[x] or {}
    local tile = self.tiles[x][y]
    if tile then
        tile.type = t
        tile.tag = tileType[t]
    else
        tile = {type = t,tag = tileType[t],x= x,y=y}
        if self.world then
            self.world:add(tile,(x-1)*self.size,(y-1)*self.size,self.size,self.size)
        end
    end
    self.tiles[x][y] = tile
    if SERVER and self.game.state == "run" then
        net:sendToRoom(self.game.room,"set_tile",{x,y,t})
    elseif not SERVER then
        self:drawTile(x,y,t)
    end
end

function map:random(w,h)
    self.w = w or self.w
    self.h = h or self.h
    if not SERVER then
        self:newCanvas()
    end
    self.tiles = {}
    for x = 1, self.w, 2 do
        for y = 1, self.h,2 do
           local t = math.random(1,6)
           self:setTile(x+1,y,t)
           self:setTile(x,y,t)
           self:setTile(x+1,y+1,t)
           self:setTile(x,y+1,t)
        end
    end
    self:makeBorder()
    if not SERVER then
        self.game:initCam()
    end
end


function map:brick(w,h)
    self.w = w or self.w
    self.h = h or self.h
    if not SERVER then
        self:newCanvas()
    end
    self.tiles = {}
    for x = 1, self.w do
        for y = 1, self.h do
            self:setTile(x,y,2)
        end
    end
    self:makeBorder()
    if not SERVER then
        self.game:initCam()
    end
end


local size = 32

function map:drawTile(x,y,t)
    love.graphics.setColor(1,1,1)
    love.graphics.setCanvas(self.map_canvas)
    texture:drawTile(t,(x-1)*self.size,(y-1)*self.size,0,self.size/8,self.size/8)
    love.graphics.setCanvas()
    if t == 1 then
        love.graphics.setCanvas(self.forest_canvas)
        texture:drawTile(t,(x-1)*self.size,(y-1)*self.size,0,self.size/8,self.size/8)
        love.graphics.setCanvas()
    elseif t == 6 then
        love.graphics.setCanvas(self.map_canvas)
        love.graphics.setColor(0, 0, 0, 1)
        love.graphics.rectangle("fill", (x-1)*self.size,(y-1)*self.size,self.size,self.size)
        love.graphics.setCanvas()
    end
end

function map:drawForest()
    love.graphics.setColor(1,1,1)
    love.graphics.draw(self.forest_canvas)
end

function map:draw()
    love.graphics.setColor(1,1,1)
    love.graphics.draw(self.map_canvas)
end



return map